#include "lcombobox.h"
#include "dropdown.h"
#include <QDebug>
#include <QEvent>
#include <QGraphicsDropShadowEffect>


LComboBox::LComboBox(QWidget* parent)
    : QWidget(parent)
{
//    this->setWindowFlags(Qt::Window);   //设置无边框窗口
    QWidget *parentwidget=this->parentWidget();
//    QPoint globalPos = parentWidget->mapToGlobal(QPoint(0,0));//父窗口绝对坐标

    pushButton = new QPushButton(parentwidget);
    connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
    pushButton->installEventFilter(this);//部件事件过滤的注册
    pushButton->setStyleSheet("border: 1px solid #666666;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");
//    pushButton->move(globalPos.x()+100,globalPos.y()+100);
    pushButton->setGeometry(100,100,pushButtonW,pushButtonH);
//    int x2 = pushButton->x();
//    int y2 = pushButton->y();
//qDebug()<<__LINE__<<"x="<<x2<<"y="<<y2;
    QFont font;
    font.setFamily(QStringLiteral("Microsoft YaHei"));//设置字体
    font.setPixelSize(15);//字体大小
    font.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100
    pushButton->setFont(font);

    //初始化定时器对象
    ifunfoldtime=new QTimer(this);
    //设置定时时间
    ifunfoldtime->setInterval(500);  //0.5秒
    //关联timeout()信号与自定义的槽函数
    connect(ifunfoldtime,SIGNAL(timeout()),this,SLOT(ifunfoldtimer()));


    dropDown = new DropDown(this);
    connect(dropDown,SIGNAL(sendDropDown(int,QString)),this,SLOT(receDropDown(int,QString)));
//    dropDown->setParentButton(pushButton);
    dropDown->hide();


    QPoint g_p = mapToGlobal(pushButton->pos());	//控件左上角在显示器的坐标
    int x = g_p.x();
    int y = g_p.y();
    int w = pushButton->width();
    int h = pushButton->height();
    buttonIcon = new QPushButton(parentwidget);
    connect(buttonIcon,SIGNAL(clicked()),this,SLOT(on_buttonIcon_clicked()));
    buttonIcon->installEventFilter(this);//部件事件过滤的注册
    buttonIcon->resize(14, 8);
    buttonIcon->move(x+w-12-12,y+(h/2)-(4));
    buttonIcon->setStyleSheet("border-radius: 1px;");
    buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));



}

LComboBox::~LComboBox()
{

}

//获取选中文本
QString LComboBox::text()
{
    return pushButton->text();
}

//设置下拉框大小
void LComboBox::size(int w, int h)
{
    dropDown->size(w, h);
}

//设置控件的显示位置
void LComboBox::move(int x, int y)
{
    pushButton->move(x, y);

    QPoint g_p = mapToGlobal(pushButton->pos());	//控件左上角在显示器的坐标
    int x2 = g_p.x();
    int y2 = g_p.y();
    int w = pushButton->width();
    int h = pushButton->height();
    buttonIcon->resize(14, 8);
    buttonIcon->move(x2+w-12-12,y2+(h/2)-(4));
}

//设置控件位置和大小
void LComboBox::setGeometry(int x, int y, int w, int h)
{
    pushButton->setGeometry(x, y ,w ,h);

//    QPoint g_p = mapToGlobal(pushButton->pos());	//控件左上角在显示器的坐标
//    int x2 = g_p.x();
//    int y2 = g_p.y();
//    int w2 = pushButton->width();
//    int h2 = pushButton->height();
    buttonIcon->resize(14, 14);
//    buttonIcon->move(x2+w2-12-12,y2+(h2/2)-(6));
    buttonIcon->move(x+w-12-12,y+(h/2)-(6));

    pushButtonW = w;
    pushButtonH = h;

    ListWidgetItemW = w;
}

//设置下拉框文本
void LComboBox::setTexe(QStringList list)
{
    dropDown->clear();

    for(int i=0;i<list.size();i++)
    {
        addText(list.at(i));
    }
//    dropDown->setTexe(list);
}

//清空下拉框内容
void LComboBox::clear()
{
    dropDown->clear();
}

//追加文本到下拉框
void LComboBox::addText(QString text)
{
    int count = dropDown->count();
    if(count < maxCount_)
    {
//        QPoint g_p = mapToGlobal(pushButton->pos());	//控件左上角在显示器的坐标
//        int x = g_p.x();
//        int y = g_p.y();

//        dropDown->move(x-5, y-5+pushButtonH+8);
        dropDown->size(ListWidgetItemW, ListWidgetItemH*(count+1));
    }
    dropDown->addText(text);
//    qDebug()<<__LINE__<<"count="<<count;
}

void LComboBox::ListWidgetItemSize(int w, int h)
{
    ListWidgetItemW = w;
    ListWidgetItemH = h;

    dropDown->ListWidgetItemSize(w, h);
}

int LComboBox::count()
{
    return dropDown->count();
}

void LComboBox::reset()
{
    pushButton->setText("");
    dropDown->setPitchOn();

    sendLComboBox(1, "");
}

void LComboBox::hide2()
{
    buttonIcon->hide();
    pushButton->hide();
}

void LComboBox::show2()
{
    buttonIcon->show();
    pushButton->show();
}

void LComboBox::setCurrentRow(int index)
{
    QString data = dropDown->setCurrentRow(index);
    pushButton->setText(data);
}

void LComboBox::setCurrentRow(QString data)
{
    dropDown->setCurrentRow(data);
    pushButton->setText(data);
}

void LComboBox::receDropDown(int n, QString data)
{
    if(n == 1)
    {
        //选中文本
        ifunfold = false;
        pushButton->setText(data);

        buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
        pushButton->setStyleSheet("border: 1px solid #666666;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");

        sendLComboBox(1, data);
    }
    else if(n == 2)
    {
        //点击下拉框外面
//        ifunfold = false;
        ifunfoldtime->start();
        buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
        pushButton->setStyleSheet("border: 1px solid #666666;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");
    }
//    else if(n == 3)
//    {
//        qDebug()<<__LINE__;
//    }
}

void LComboBox::ifunfoldtimer()
{
    ifunfoldtime->stop();
    ifunfold = false;
}

void LComboBox::on_pushButton_clicked()
{
    if(!ifunfold)
    {
        ifunfoldtime->stop();

        ifunfold = true;

//        QPoint g_p = mapToGlobal(pushButton->pos());	//控件左上角在显示器的坐标
//        int x = g_p.x();
//        int y = g_p.y();
//        int w = pushButton->width();
//        int h = 50 * 5;
//    //    dropDown->setGeometry(x-5,y-5,194,130);
//        dropDown->move(x-5, y-5+pushButtonH+8);
//        dropDown->size(w, h);

        QPoint g_p = mapToGlobal(pushButton->pos());	//控件左上角在显示器的坐标
        int x = g_p.x();
        int y = g_p.y();

        dropDown->move(x-5, y-5+pushButtonH+8);

        buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/22.png"));
        dropDown->show();


    }
    else
    {
        ifunfold = false;
        buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
    }
}

void LComboBox::on_buttonIcon_clicked()
{
    if(pushButton->text().isEmpty())
    {
        on_pushButton_clicked();
    }
    else
    {
        buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));

        pushButton->setText("");
        dropDown->setPitchOn();

        sendLComboBox(1, "");
    }
}

bool LComboBox::eventFilter(QObject *watched, QEvent *event)
{
    if(pushButton==watched)
    {
        QWidget *parentwidget=this->parentWidget();
        if(QEvent::Enter==event->type())
        {
            pushButton->setStyleSheet("border: 1px solid #1B89CC;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");
            parentwidget->setCursor(Qt::PointingHandCursor); //设置鼠标样式
            if(pushButton->text().isEmpty())
            {
//                if(ifunfold)
//                {
//                    buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/22.png"));
//                }
//                else
//                {
//                    buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
//                }

            }
            else
            {
                buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/117.png"));
            }
        }
        else if(QEvent::Leave==event->type())
        {
            parentwidget->setCursor(Qt::ArrowCursor); //设置鼠标默认样式
            if(ifunfold)
            {
                buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/22.png"));
            }
            else
            {
                buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
                pushButton->setStyleSheet("border: 1px solid #666666;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");
            }
        }
    }
    if(buttonIcon==watched)
    {
        QWidget *parentwidget=this->parentWidget();
        if(QEvent::Enter==event->type())
        {
            pushButton->setStyleSheet("border: 1px solid #1B89CC;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");
            parentwidget->setCursor(Qt::PointingHandCursor); //设置鼠标样式
            if(pushButton->text().isEmpty())
            {
//                if(ifunfold)
//                {
//                    buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/22.png"));
//                }
//                else
//                {
//                    buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
//                }
            }
            else
            {
                buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/117.png"));
            }
        }
        else if(QEvent::Leave==event->type())
        {
//            pushButton->setStyleSheet("border: 1px solid #666666;border-radius: 8px;font-size: 15px;font-family: Microsoft YaHei;color: #333333;background-color: #FFFFFF;text-align: left center;padding-left:13px;");
//            parentwidget->setCursor(Qt::ArrowCursor); //设置鼠标默认样式
//            buttonIcon->setIcon(QPixmap(":/new/prefix1/resource/10.png"));
        }
    }
    return QObject::eventFilter(watched,event);
}
